"""
爬取淘票票网站中的所有城市和其拼音
"""
import urllib.request

# url = 'https://dianying.taobao.com/cityAction.json?activityId&_ksTS=1610959154784_137&jsoncallback=jsonp138&action=cityAction&n_s=new&event_submit_doGetAllRegion=true'
#
# headers = {
# 	# 'User-Agent':'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
# 	'accept': 'text/javascript, application/javascript, application/ecmascript, application/x-ecmascript, */*; q=0.01',
# 	'accept-language': 'zh-CN,zh;q=0.9',
# 	'cookie': 't=fcdf6f8247310c1823ba88fc9b026797; cookie2=1b8856ff0127f65809eff67589ea1c9c; v=0; _tb_token_=e5933ebe3ede6; cna=mul+GOXyrCICARsJVqr1rcx7; xlly_s=1; l=eBQEsHOejOyrZUAfBO5anurza77t5IRbzsPzaNbMiInca61F9FTAYNCINWQHWdtjgtfX8etzuK-oqRnBrezp0giMW_N-1NKm6xJw-; tfstk=cfKRBxwwYjcu3i6vQU30OTcgS0DcZcgRtjfavAY-lo8q4MqdijKMXCb0mOFRDxC..; isg=BJaWPzM76mQV2d7AeCCBjPY250yYN9pxI9vawQD_ZXkTwzRdaMJ4geS1W18v69KJ',
# 	'referer': 'https://dianying.taobao.com/',
# 	'sec-fetch-dest': 'empty',
# 	'sec-fetch-mode': 'cors',
# 	'sec-fetch-site': 'same-origin',
# 	'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36',
# 	'x-requested-with': 'XMLHttpRequest',
# }
#
# request = urllib.request.Request(url=url, headers=headers)
# response = urllib.request.urlopen(request)
# resp_data = response.read().decode('utf-8')
#
# with open('movie.json', 'w',encoding='utf-8') as file:
# 	file.write(resp_data)


# 解析
import json
import jsonpath

# json.load 是针对于 文本 操作的，将 json 格式文本 转换为 python 对象
# json.loads 针对 字符串 操作的，将 json 字符数据转换为 python 对象，一般为 list 或 dict
obj = json.load(open('movie.json', 'r', encoding='utf-8'))

# 所有城市 和其 拼音
city_list = jsonpath.jsonpath(obj, '$..regionName')
city_pinyin = jsonpath.jsonpath(obj, '$..pinYin')

# 一一对应
for i in range(len(city_list)):
	city = city_list[i]
	pinyin = city_pinyin[i]

	# 以字典 形式写入文件
	c = {}
	c['city'] = city
	c['pinyin'] = pinyin

	# a 为追加写入，不会覆盖原写入的内容
	# write() 不能写入 字典 类型，可以 使用 str 转换成字符形式
	with open('m_movie.json', 'a', encoding='utf-8') as file:
		file.write(str(c))

